#include <stdio.h>
#include <stdlib.h>
#define TRIADSIZE 100
#define M 3
#define N 4
typedef int ElemType;
typedef struct Triad{
	int i,j;
	ElemType v;
};
typedef struct SqTriadTable{
	struct Triad data[TRIADSIZE];
	int m,n,k;
};
void zhuanzhi(SqTriadTable *T){
    int temp;
    for(int i=0;i<T->k;i++){
        temp=T->data[i].i;
        T->data[i].i=T->data[i].j;
        T->data[i].j=temp;
    }

}
void CreateSqTriadTable(SqTriadTable *T,ElemType A[M][N]){
	int i,j,k=0;
	for(i=0;i<M;i++)
		for(j=0;j<N;j++)
			if(A[i][j]!=0){
				T->data[k].i=i;
				T->data[k].j=j;
				T->data[k].v=A[i][j];
				k++;
			}
	T->m=M;
	T->n=N;
	T->k=k;
}
int main(){
	int i,j,m;
	SqTriadTable *T;
	ElemType A[M][N];
	ElemType b[N][M];
	T=(SqTriadTable *)malloc(sizeof(SqTriadTable)); 
	printf("请依次输入%d*%d的稀疏矩阵中的元素值(以空格键隔开，回车键换行):\n",M,N);
	for(i=0;i<M;i++)
		for(j=0;j<N;j++)
			scanf("%d",&A[i][j]);
	CreateSqTriadTable(T,A);
	printf("三元组表为：\n");
	for(m=0;m<T->k;m++)
		printf("(%3d,%3d,%3d)\n",T->data[m].i,T->data[m].j,T->data[m].v);
    zhuanzhi(T);
    // for(m=0;m<T->k;m++)
	// 	printf("(%3d,%3d,%3d)\n",T->data[m].i,T->data[m].j,T->data[m].v);
    printf("转置后的矩阵：\n")
    for(m=0;m<T->k;m++)
        b[T->data[m].i][T->data[m].j]=T->data[m].v;
           
    for(i=0;i<N;i++){
		for(j=0;j<M;j++)
			printf("%d ",b[i][j]);
        printf("\n");    
        }
	return 0;
}
